// pages/otw/otw.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleBarHeight:"",
    list:[],
    text:'点击加载更多',
    num:6,
    data:'',
    off:0,
    sef:0,
    des:56,
    more:"",
    total:'',
    country:'',
    activeclass:'shi',
    imgdix:"",
    titlename:'',
    images:'',
    datas:[],//添加的数组
    jiage:0,//总价格
    geshu:0,//总数量
    arr:[],
    title:'套餐一',
    flag:false
    
    
  },
  //点击返回上一页

  deri(e) {
    if (wx.getStorageSync('tk') == '123') {
      console.log(e)
      wx.navigateTo({
        url: '/pages/works/index?id=' + e.currentTarget.dataset.id.wineid + "&type=1",
      })
    } else {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }


  },
  //点击增加数量h
  add(e) {
    if (wx.getStorageSync('tk') == '123') {

      let index = e.currentTarget.dataset.id
      if(this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num+1 ==e.target.dataset.item.stock){
        console.log("上限了")
      }
      let money = 0
      this.data.datas.push(e.currentTarget.dataset.item)
      this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num = this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num + 1
      console.log()
      console.log(this.data.datas)
      // this.data.list[0].map(item => {
      //   item.list.map(items=>{
      //      money += items.choice_num * items.price;
      //   })
      // })
      this.data.datas.map(item =>{
       
        money+=item.price
        
      })
      console.log(money)
      this.setData({
        list: this.data.list,
        geshu: this.data.geshu + 1,
        datas: this.data.datas,
        jiage: Number(money).toFixed(2),
      })
      console.log(this.data.datas)
    } else {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }


  },
  //点击减少数量
  adds(e) {
    if (wx.getStorageSync('tk') == '123') {
      let money=0
      this.data.datas.map(item =>{
        item.choice_num--
        money+=item.choice_num*item.price
      })
      console.log(this.data.datas)
      // console.log(ind)
      let ind = this.data.datas.findIndex(item =>item.id == e.currentTarget.dataset.item.id)
      this.data.datas.splice(ind,1)
      this.setData({
        datas: this.data.datas
      })
      let index = e.currentTarget.dataset.id
      if (this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num == 0) {
        this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num == 0
        this.setData({
          list: this.data.list
        })
      } else {
        let money = 0;
        this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num = this.data.list[e.currentTarget.dataset.indexs][e.currentTarget.dataset.index].list[index].choice_num - 1
        // this.data.list[0].map(item => {
       
        //   item.list.map(items=>{
        //      money += items.choice_num * items.price;
        //   })
         
        // })
        this.data.datas.map(item => {
          money += item.price
        })
        this.setData({
          list: this.data.list,
          geshu: this.data.geshu - 1,
          jiage: money.toFixed(2),

        })
      }
    } else {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }
  },
  //点击购买跳转页面
  purchase() {
    console.log(this.data.list,'jshdfbjlsd')
    let arrlist = []
    this.data.list.map(items=>{
      items.forEach(item =>{
        item.list.forEach(items =>{
          arrlist.push(items)
        })
    })
    })
    
    let arr=(arrlist.filter(itema => itema.choice_num >= 1))
    console.log(arr,9090)
    if (this.data.jiage == 0) {
      wx.showToast({

        title: '请选择商品',

        icon: 'success',

        duration: 2000 //持续的时间

      })
    } else {
      let that=this;

      app.request({
        url:'/www',
        data:{
        "text1":arr[0].wineName,
        "text2":this.data.geshu,
        "pic":wx.getStorageSync('avatarurl'),
        "name":wx.getStorageSync('name')
        },
        method:'post',
        success: function (res) {
          wx.setStorageSync('xiao', '123')
          wx.setStorageSync('fenx', res.pic)
          
        }
      })
      
    }
wx.setStorageSync('arr', this.data.datas)
        wx.setStorageSync('pic', this.data.jiage)
        wx.navigateTo({
          url: '../purchase/purchase',
        })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  userlist:function(){
    let that=this;
    app.request({
      url:'/owtOrder/getOwtOrderPage',
      data:{
        'offset':this.data.sef,
'num': 5
      },
      method:'post',
      success: function (res) {
        that.data.arr.push(res.data.records)
        that.setData({
          arr:that.data.arr,
          sef:res.data.offset
        })
      }
    })
  },
  onLoad: function (options) {
    let that=this;
    let xiao=wx.getStorageSync('xiao')
    if(xiao=='123'){
      that.setData({
        images:wx.getStorageSync('fenx')
      })
    }
    app.request({
      url:'/owtOrder/getOwtOrderPage',
      data:{
        'offset':this.data.off,
'num': 5
      },
      method:'post',
      success: function (res) {
        that.data.arr.push(res.data.records)
        that.setData({
          arr:that.data.arr,
          sef:res.data.offset
        })
      }
    })
    app.request({
      url:'/owtPlatform/OwtPlayCountryName',
      method:'post',
      success: function (res) {
        that.setData({
          titlename:res.data
        })
      }
    })
    this.list()



  },
  switchTaps(){ //更换资讯大类
   
   this.setData({
     activeclass:'shi',
     text:'点击加载更多',
     country:"",
     flag:false,
    //  geshu:0,
    //  jiage:0,
    //  datas:[],
     off:0,
     list:[]
   })
  //  wx.removeStorageSync('arr')
  //  wx.removeStorageSync('pic')
   let that=this
    
   app.request({
     url: '/owtPlatform/OwtPlatNameList',
     data: {
       'offset': 0,
       'country':this.data.country,
       'num': 4
     },
     method: 'post',
     success: function (res) {
      res.data.records.map(item => {
        item.list.map(items=>{
           console.log(items)
           that.data.datas.map(itemval =>{
             if(items.id == itemval.id){
               items.choice_num++
             }
           })
        })
      })
         that.data.list.push(res.data.records)
       that.setData({
         list:  that.data.list,
         more:res.data.more,
         off: res.data.offset,
         flag: true
       })
       window.scrollTop = 0
       console.log(that.data.list)
      
    

     }
   });
  },
  switchTap(e){ //更换资讯大类
    console.log(e)
   this.setData({
     activeclass:e.currentTarget.dataset.index,
     country:e.currentTarget.dataset.type,
     text:'点击加载更多',
     flag: false,
    //  geshu:0,
    //  jiage:0,
    //  datas:[],
     list:[]
   })
  //  wx.removeStorageSync('arr')
  //  wx.removeStorageSync('pic')
   console.log(this.data.activeclass)
   let that=this
    
   app.request({
     url: '/owtPlatform/OwtPlatNameList',
     data: {
       'offset': 0,
       'country':this.data.country,
       'num': 5
     },
     method: 'post',
     success: function (res) {
     
         that.data.list.push(res.data.records)
       
       that.data.list[0].map(item => {
        item.list.map(items=>{
           console.log(items)
           that.data.datas.map(itemval =>{
             if(items.id == itemval.id){
               items.choice_num++
             }
           })
        })
       
      })
      // let money = 0
      // that.data.list[0].map(item => {
      //   item.list.map(items=>{
      //      money += items.choice_num * items.price;
      //   })
      // })
       console.log(that.data.datas,that.data.list)
       that.setData({
        list:  that.data.list,
        more:res.data.more,
        off: res.data.offset,
        flag:true
        // jiage: money.toFixed(2),
      })
      window.scrollTop = 0

     }
   });
  },

  list:function(){
    let that=this
    
    app.request({
      url: '/owtPlatform/OwtPlatNameList',
      data: {
        'offset': this.data.off,
        'country':this.data.country,
        'num': that.data.num
      },
      method: 'post',
      success: function (res) {
      console.log(res)
          that.data.list.push(res.data.records)
        that.setData({
          list:  that.data.list,
          more:res.data.more,
          off: res.data.offset,
          total:res.data.total,
          flag: true
        })
        console.log(that.data.more)
       
     

      }
    });
  },
  
  xia: function () {
     if(this.data.more==false){
      this.setData({
        text:'已经没有数据了'
      })
    }
    else{
      this.setData({
        num:this.data.total-4
      })
      console.log(this.data.num)
     this.list()
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // onPageScroll(e) { 
  //   if (e.scrollTop==0){
  //     this.setData({
  //       off: 0,
  //       des: 56
  //     })
  //     this.list()
  //   }
  // },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  scrolltoupper: function () {
    console.log(1)
  },
  scrolltolower: function () {
    if (this.data.more == true) {
      this.setData({
        off: this.data.off + 50,
        des: this.data.des + 50
      })
      this.list()
    } else {

    }

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(123)
    if (this.data.more == true) {
      this.setData({
        off: this.data.off + 50,
        des: this.data.des + 50
      })
      this.list()
    }


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'CASKHOME',
     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    }
  }
})